/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.util.Util;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class DealFieldManager
/*     */ {
/*  27 */   private static Logger logger = Logger.getLogger(DealFieldManager.class
/*  28 */     .getName());
/*     */ 
/*     */   public List<Map<String, Object>> list(int companyId)
/*     */     throws DBException, SQLException
/*     */   {
/*  40 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  41 */     if (conn == null) {
/*  42 */       throw new DBException();
/*     */     }
/*     */ 
/*  45 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  47 */       return (List)runner.query(conn, "SELECT DealFieldId, DealFieldName, ValueType, InitValue, Mandatory FROM T_Deal_Field WHERE CompanyId = ?  ORDER BY OrderNum ASC", 
/*  48 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */     } finally {
/*     */       try {
/*  51 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  53 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public static String getFieldName(int id, List<Map<String, Object>> data)
/*     */   {
/*  66 */     if ((data == null) || (data.size() == 0)) {
/*  67 */       return "";
/*     */     }
/*     */ 
/*  70 */     for (Map item : data) {
/*  71 */       if (Util.str2Int(item.get("DealFieldId")) == id) {
/*  72 */         return (String)item.get("DealFieldName");
/*     */       }
/*     */     }
/*  75 */     return "";
/*     */   }
/*     */ 
/*     */   public int add(int companyId, String name, int valueType, String initValue, int fieldMandatory, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/*  93 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  94 */     if (conn == null) {
/*  95 */       throw new DBException();
/*     */     }
/*  97 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  98 */       new ScalarHandler(), new String[] { "DealFieldId" });
/*     */     try {
/* 100 */       runner.update(conn, "INSERT INTO T_Deal_Field (DealFieldName, ValueType, InitValue, Mandatory, CompanyId, CreateDate, CreateUserId) values (?, ?, ?, ?, ?, NOW(), ?)", new Object[] { name, Integer.valueOf(valueType), 
/* 101 */         initValue, Integer.valueOf(fieldMandatory), Integer.valueOf(companyId), userId });
/* 102 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*     */     } finally {
/*     */       try {
/* 105 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 107 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int update(int dealFieldId, String name, int valueType, int fieldMandatory, String initValue, String userId)
/*     */     throws DBException, SQLException
/*     */   {
/* 128 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 129 */     if (conn == null) {
/* 130 */       throw new DBException();
/*     */     }
/* 132 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 135 */       return runner.update(conn, "UPDATE T_Deal_Field SET DealFieldName = ? , ValueType = ?, InitValue = ?, Mandatory = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE DealFieldId = ?", new Object[] { name, 
/* 136 */         Integer.valueOf(valueType), initValue, Integer.valueOf(fieldMandatory), userId, Integer.valueOf(dealFieldId) });
/*     */     } finally {
/*     */       try {
/* 139 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 141 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int delete(int dealFieldId)
/*     */     throws DBException, SQLException
/*     */   {
/* 155 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 156 */     if (conn == null) {
/* 157 */       throw new DBException();
/*     */     }
/* 159 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 162 */       int result = runner.update(conn, "DELETE FROM T_Deal_Field WHERE DealFieldId = ?", 
/* 163 */         Integer.valueOf(dealFieldId));
/*     */ 
/* 166 */       runner.update(conn, "DELETE FROM T_Deal_Ext_Info WHERE Type = ?", Integer.valueOf(dealFieldId));
/* 167 */       return result;
/*     */     } finally {
/*     */       try {
/* 170 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 172 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int updateSort(int id, int order, String userId)
/*     */   {
/* 186 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 187 */     if (conn == null) {
/* 188 */       return 0;
/*     */     }
/* 190 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/* 193 */       return runner.update(conn, "UPDATE T_Deal_Field SET OrderNum = ? , UpdateDate = NOW(), UpdateUserId = ? WHERE DealFieldId = ?", new Object[] { Integer.valueOf(order), 
/* 194 */         userId, Integer.valueOf(id) });
/*     */     } catch (SQLException e) {
/* 196 */       logger.warn("update order failed", e);
/*     */     } finally {
/*     */       try {
/* 199 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 201 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 204 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.DealFieldManager
 * JD-Core Version:    0.6.1
 */